package org.hjq.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.hjq.clients.UserClient;
import org.hjq.entity.Order;
import org.hjq.entity.PageVO;
import org.hjq.entity.User;
import org.hjq.mapper.OrderMapper;
import org.hjq.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Autowired
    private UserClient userClient;

    @Override
    public Order queryOrderById(Long orderId) {
        // 1.查询订单
        Order order = baseMapper.findById(orderId);
        // 2.用Feign远程调用
        User user = userClient.findById(order.getUserId());
        // 3.封装user到Order
        order.setUser(user);
        // 4.返回
        return order;
    }

    @Override
    public PageVO queryOrder(Long limit, Long size) {
        Page<Order> page = new Page<>(limit,size);
        page(page);
        // 获取分页结果
        List<Order> orderList = page.getRecords();
        // 获取结果总数
        long total = page.getTotal();
        return new PageVO(orderList,total);
    }

    /*@Autowired
    private RestTemplate restTemplate;

    public Order queryOrderById(Long orderId) {
        // 1.查询订单
        Order order = orderMapper.findById(orderId);
        // 2.利用RestTemplate发起http请求，查询用户
        // 2.1.url路径
        String url = "http://userservice/user/" + order.getUserId();
        // 2.2.发送http请求，实现远程调用
        User user = restTemplate.getForObject(url, User.class);
        // 3.封装user到Order
        order.setUser(user);
        // 4.返回
        return order;
    }*/
}
